package com.yzd.netmonitor.util;

import java.util.concurrent.ThreadLocalRandom;

/**
 * 随机数工具类.
 * @version 1.0.0
 * @see 1.0.0
 *
 */
public final class RandomHelper {

	/** 小写字母 */
	public final static char[] low = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
			'y', 'z' };

	/** 小写字母长度 */
	public final static int low_len = low.length;

	/** 混合随机数 */
	public final static char[] mix = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
			'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

	/** 混合随机数长度 */
	public final static int mix_len = mix.length;

	/**
	 * 小写字母随机数.
	 * 
	 * @param length
	 * @return
	 */
	public final static String low(final int length) {
		if (length <= 0) {
			return "";
		}

		ThreadLocalRandom random = ThreadLocalRandom.current();
		final char[] cs = new char[length];
		for (int j = 0, i = length; j < i; j++) {
			final char tmp = low[random.nextInt(low_len)];
			cs[j] = tmp;
		}
		return new String(cs);
	}

	/**
	 * 大写字母随机数.
	 * 
	 * @param length
	 * @return
	 */
	public final static String upperRandom(final int length) {
		return low(length).toUpperCase();
	}

	/**
	 * 小写混合随机数.
	 * 
	 * @param length
	 * @return
	 */
	public final static String mixed(final int length) {
		if (length <= 0) {
			return "";
		}

		ThreadLocalRandom random = ThreadLocalRandom.current();
		final char[] cs = new char[length];
		for (int j = 0, i = length; j < i; j++) {
			final char tmp = mix[random.nextInt(mix_len)];
			cs[j] = tmp;
		}
		return new String(cs);
	}

	/**
	 * 大写混合随机数.
	 * 
	 * @param length
	 * @return
	 */
	public final static String upMixed(final int length) {
		return mixed(length).toUpperCase();
	}

	/**
	 * 指定最大值的随机数, 最小为0.
	 * 
	 * @param bound
	 * @return
	 */
	public final static int nextInt(final int bound) {
		return nextInt(0, bound);
	}

	/**
	 * 指定范围的随机数.
	 * <p>
	 * 例如: least = 9999, bound = 99999, 此时产生的随机数范围: 9999 < value < 99999.
	 * </p>
	 * 
	 * @param least
	 * @param bound
	 * @return
	 */
	public final static int nextInt(final int least, final int bound) {
		final ThreadLocalRandom random = ThreadLocalRandom.current();
		return random.nextInt(least, bound);
	}

	private RandomHelper() {
		throw new AssertionError("Uninstantiable class.");
	}
}
